<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="css/jquery-ui-1.10.3.custom.min.css">
    <link rel="stylesheet" type="text/css" href="css/ui.jqgrid.css">
    <link rel="stylesheet" type="text/css" href="css/mycss.css">
    <style>
.ui-icon-plus {
	background-position: -16px -128px;
}
</style>
    <script src="js/jquery-1.9.0.min.js" type="text/javascript"></script>
    <script src="js/grid.locale-he.js" type="text/javascript"></script>
    <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.10.3.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">

        $(document).ready(function () {
		$("#agentList").jqGrid({
             url:"gridFunctions/agentActions.php",
			 editurl: "gridFunctions/agentActions.php",
			 direction: 'rtl',
			 async: false,
			 datatype: 'json',
     		 pager: '#pager',
             colNames: ['מספר סוכן','ת"ז', 'שם פרטי', 'שם משפחה','מייל', 'עיר', 'רחוב ובית', 'טלפון', 'פקס', 'פלאפון', 'סיסמא', 'חברה', 'מחירון', 'סוכן מטריה', 'סוכן בוס'],
             colModel: [
			{ name: 'person_number', index: 'person_number', width: 60, align: 'left',  editable: false ,align:'right' },
            { name: 'person_id', index: 'person_id', width: 60, align: 'left', editable: true,align:'right',editrules:{required:true,integer:true} },
            { name: 'firstname', index: 'firstname', width: 120, align: 'left', editable: true,align:'right' },
		    { name: 'lastname', index: 'lastname', width: 120, align: 'left', editable: true,align:'right' },
			{ name: 'email', index: 'email', width: 120, align: 'left', editable: true,align:'right' },
            { name: 'city', index: 'city', width: 70, align: 'left', editable: true,align:'right' },
            { name: 'address', index: 'address', width: 70, align: 'left', editable: true,align:'right' },
            { name: 'telephone', index: 'telephone', width: 80, align: 'left', editable: true,align:'right' },
            { name: 'fax', index: 'fax', width: 60, align: 'left', editable: true,align:'right' },
            { name: 'cellphone', index: 'cellphone', width: 180, align: 'left', editable: true,align:'right'},
            { name: 'pass_word', index: 'pass_word', width: 70, align: 'left', editable: true,align:'right' },
            { name: 'companyname', index: 'companyname', width: 70, align: 'left', editable: true,align:'right' },
            { name: 'mehiron_number', index: 'mehiron_number', width: 80,align:'right' },
            { name: 'metria', index: 'metria', width: 60, align: 'left', editable:true, edittype:'checkbox', editoptions: { value:"1:0"}, 
  formatter: "checkbox",align:'right' },
			{ name: 'underagent', index: 'underagent', width: 180, align: 'left',editable: true,edittype:"select",editoptions:{},align:'right'}],
           
		    loadComplete: function() {
			$.ajax({ url: 'gridFunctions/agentActions.php',
			data: {oper:'getMetriaAgent'},
			type: 'post',
			success: function(output) 
				{
				output = output.substr(0, output.length-1); 
				output = output.substr(1);
				output=output.replace(/{/g,"");
				output=output.replace(/}/g,"");
				output='{"0":"עצמאי",'+output+"}";
				console.log(output); 
				var catagentList=eval('(' + output + ')');		   
				jQuery("#agentList").setColProp('underagent', { editoptions: { value: catagentList} }) 
				}
			});	//end og ajax call
			},
		    pager: jQuery('#pager'),
            rowNum: 20,
            rowagentList: [5, 10, 20, 50],
            sortname: 'person_number',
            sortorder: "desc",
            mtype: "POST",
			ajaxSelectOptions: { cache: false },
		//loadonce: true,
			postData:{oper:'view'},
            viewrecords: true,
            imgpath: '../../Scripts/css/Aristo/images',
            autowidth: true,
            height: "100%"
            }); // -- end of   $("#agentList").jqGrid({
				
			$("#agentList").jqGrid('navGrid', '#pager', {edit:true,add:true,del:true,refresh:true, closeAfterAdd: true,reloadAfterSubmit:true, search:false},
      // Edit dialog parameters
       {
		reloadAfterSubmit: true,
        closeAfterEdit: true,
		//editData: {person_number: function() {  return "7"; } },
		onclickSubmit: function (rp_ge, postdata) 
		{
			return { person_number: getSelectedCell('person_number') };
		},
		afterSubmit: function(response,postdata)
		{ 
			if(response.responseText=="okok")
			{
			success=true;
			alert("פרטי הלקוח התעדכנו בהצלחה")
			}
			else 
			{
				
			success = false;
			alert("פרטי הלקוח לא התעדכנו בהצלחה")
			}
			return [success,response.responseText] 
        }
      },
      // Add dialog parameters
      {
		onclickSubmit: function (rp_ge, postdata) 
		{
			return { person_number: getSelectedCell('person_number') };
		},
		recreateForm: true ,
		reloadAfterSubmit: true,
        closeAfterAdd: true,
		afterSubmit: function(response,postdata){ 
			if(response.responseText=="okok")
			{
			success=true;
			alert("סוכן התווסף בהצלחה");
			}
			else 
			{
			success = false;
			alert("סוכן לא התווסף בהצלחה");
			}
			return [success,response.responseText];
        }
		
      },
      // Delete dialog parameters
      {
	  reloadAfterSubmit: true,
	  onclickSubmit: function (rp_ge, postdata) 
		{
			return { person_number: getSelectedCell('person_number') };
		},
		afterSubmit: function(response,postdata){ 
			if(response.responseText=="okok")
			{
			success=true;
			alert("סוכן נמחק בהצלחה")
			}
			else 
			{
			success = false;
			alert("סוכן לא נמחק בהצלחה")
			}
			return [success,response.responseText] 
        }
	
	  },
      // Search dialog parameters
      {},
      // View dialog parameters
      {}
);

$('#agentList').jqGrid('filterToolbar', { searchOnEnter: true, enableClear: false });
	

	
  });// -- end of  $(document).ready(function ()
function getSelectedCell(str)
{
var grid = jQuery('#agentList');
var sel_id = grid.jqGrid('getGridParam', 'selrow');
var myCellData = grid.jqGrid('getCell', sel_id,str);
return myCellData;
}
        

               
    </script>
    <title>ניהול סוכנים</title>
    </head>

<body dir="rtl"  style="padding:0px; margin:0px;">
<div id="main">
<?php include("adminHeader.php"); ?>
<h1>רשימת סוכנים</h1>
<div id="lc">
      <table id="agentList" class="scroll" cellpadding="0" cellspacing="0">
  </table>
      <div id="pager" class="scroll" style="text-align: center;"> </div>
    </div>
<?php include("../footer.php"); ?>	
</div>
</body>
</html>